// 订单商品
var express = require('express');
var router = express.Router();
const orderGoodsService = require('../service/orderGoodsService');

//增加匹配 
router.post('/', async function (req, res) {

    // 自动生成当前的时间戳
    function getLastDate() {
        var date = new Date();
        return Date.parse(date) / 1000;
    }

    req.body.time = getLastDate();
    console.log(req.body);
    let data = await orderGoodsService.insert(req.body)
    res.send(data);
})



//删除匹配
router.delete('/:id', async function (req, res) {
    let data = await orderGoodsService.delete(req.params.id)
    res.send(data);
})

//修改匹配
router.put('/putId', async function (req, res) {
    let id = req.body.id
    console.log(id);
    let data = await orderGoodsService.update(id, req.body)
    res.send(data);
})




//修改匹配
router.put('/:id', async function (req, res) {
    // 当修改的数据只是预约时间时   ， req.body.orderTime 格式必须为 2021-06-19 22:33:00
    if (req.body.orderTime) {
        // 获取预约时间戳
        let orderDate = new Date(req.body.orderTime);
        function orderTime(orderDate) {
            console.log("预约时间", orderDate);
            return Date.parse(orderDate) / 1000;
        }
        req.body.orderTime = orderTime(orderDate);
    }
    let data = await orderGoodsService.update(req.params.id, req.body)
    res.send(data);
})




//分页所有/条件查询
router.get('/all', async function (req, res) {
    console.log("req", req.query);
    let { type, value, current, size, typeId, valueId } = req.query;
    console.log("type, value", type, value);
    let searchObj = {};
    if (type) {
        searchObj[type] = { $regex: value } // 使用正则可以实现模糊查询
    } else if (typeId) {
        searchObj[typeId] = valueId
    }
    let data = await orderGoodsService.findNo(searchObj, { current: current || 1, size: size || 5 })
    res.send(data);
})



module.exports = router;









